import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import com.sun.org.apache.regexp.internal.RE;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/3/18
 * \* Time: 10:52
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class Select {
    //1.创建数据库
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");
    //2.连接数据库
        Connection connection = dataSource.getConnection();
    //3.操作数据库
        String sql = "select * from student";

        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();

        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id=" + id + ",name=" +name);
        }
    //释放资源，
        resultSet.close();
        statement.close();
        connection.close();
    }
}
